﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

/**
*    @file:    usercontroller.cpp
*    @brief:
*    @author:  sanshi522@163.com
*    @date:    2024-11-03  14:57
*/
#include "usercontroller.h"
#include "user.h"

#include <QJsonDocument>
#include <qjwtutils.h>
#include <responesutils.h>
#include <userservice.h>

UserController::UserController(QObject *parent): HttpRequestHandler(parent)
{

}

UserController::~UserController()
{

}

void UserController::service(HttpRequest &request, HttpResponse &response)
{
    QString path = request.getPath();
    QStringList pathList = path.split("/");
    if (pathList.size() < 3)
    {
        this->returnError(response);
        return;
    }
    QString method = pathList.value(3);
    QByteArray responseMsg;
    QString methodType = request.getMethod();

    qDebug() << __FUNCTION__ << methodType;

    if (methodType == "GET")
    {
        if (method == "info")
        {
            return getUserInfo(request, response);
        }
        if (method == "list")
        {
            return getList(request, response);
        }
        if (method == "pagelist")
        {
            return getPageList(request, response);
        }
    }
    else if (methodType == "POST")
    {
        if (method == "save")
        {
            return saveOne(request, response);
        }
    }
    else if (methodType == "DELETE")
    {
        if (method == "delete")
        {
            return deleteOne(request, response);
        }
    }
    this->returnError(response);
}

void UserController::getUserInfo(HttpRequest &request, HttpResponse &response)
{
    QString authHeader = request.getHeader("Authorization");
    //权限找不到或者权限字符串头找不到
    if(authHeader.isEmpty() || authHeader.indexOf("Bearer ") == -1 || authHeader.split(" ").size() < 2){
        qDebug() << "权限找不到或者权限字符串头找不到";
        ResponesUtils::unAuthorizedResponse(response);
        return;
    }
    QString token = authHeader.split(" ")[1];
    QJsonObject jsonObject= QJwtUtils::verifyAccessToken(token);
    User user;
    user.fromJsonObject(jsonObject);
    if(user.isEmpty()){
        ResponesUtils::unAuthorizedResponse(response);
        return;
    }
    qDebug()<< __FUNCTION__ << user.toJsonObject();
    ResponesUtils::resultResponse(response,user.toJsonObject());
}

void UserController::getList(HttpRequest &request, HttpResponse &response)
{
    UserService objectService;
    QList<User> list;
    if(!objectService.getList(list)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }

    QJsonArray jsonArray ;
    for(User &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::resultResponse(response,jsonArray);
}

void UserController::getPageList(HttpRequest &request, HttpResponse &response)
{
    uint64_t page = request.getParameter("page").toULongLong();
    uint64_t pageSize = request.getParameter("pageSize").toULongLong();
    if(page == 0 || pageSize == 0){
        return ResponesUtils::erroResponse(response,"page and pageSize and  are required");
    }
    UserService objectService;
    uint64_t total=0;
    QList<User> list;
    if(!objectService.getPageList(total,list,page,pageSize)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }

    QJsonArray jsonArray ;
    for(User &item:list){
        jsonArray.append(item.toJsonObject());
    }
    return ResponesUtils::pagedResultResponse(response,total,jsonArray);
}

void UserController::saveOne(HttpRequest &request, HttpResponse &response)
{
    QByteArray body = request.getBody();
    QJsonParseError parseError;
    QJsonDocument  jsonDocument= QJsonDocument::fromJson(body, &parseError);

    if (parseError.error != QJsonParseError::NoError)
    {
        return ResponesUtils::erroResponse(response,"请求参数JSON结构异常!");
    }
    User object;
    object.fromJsonObject(jsonDocument.object().value("params").toObject());
    UserService objectService;
    if(!objectService.saveOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"添加成功！");
}

void UserController::deleteOne(HttpRequest &request, HttpResponse &response)
{
    uint64_t id = request.getParameter("id").toULongLong();
    qDebug()<< id;
    if(id == 0 ){
        return ResponesUtils::erroResponse(response,"请指定删除ID！");
    }
    User object;
    object.setId(id);
    UserService objectService;
    if(!objectService.deleteOne(object)){
        return ResponesUtils::erroResponse(response,"数据库异常！");
    }
    //返回结果
    ResponesUtils::resultResponse(response,"删除成功！");
}
